package org.vectrics.cms;

import org.apache.ecs.html.A;
import org.apache.ecs.html.TD;
import org.apache.ecs.html.TR;
import org.apache.ecs.html.Table;
import org.vectrics.common.util.StringUtils;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;


public class ContentTag extends VectricsTagSupport {

	private static final long serialVersionUID = 2122027924435152711L;
	
	public static String TRUE = "true";
	public static String FALSE = "false";
	private String section = null;
	private String type = "text";
	private String code = null;
	private String domainId = null;
	
	public String getDomainId() {
		return domainId;
	}

	public void setDomainId(String domainId) {
		this.domainId = domainId;
	}
	
	public Domain getDomain() {
		Domain domain = null;
		if (StringUtils.isNullOrBlank(this.getDomainId())) {
			domain = CoreServiceLocator.getSystemService().findSystemDomain();
		} else {
			domain = CoreServiceLocator.getSystemService().findDomain(new Long(this.getDomainId()));
		}
		return(domain);
	}

	protected String makeStartTag() {
		boolean editMode = false;
		if (this.getEdit()) {
			editMode = true;
		}
		
		Table table = new Table();
		table.setCellPadding(0);
		table.setCellSpacing(0);
		table.setWidth("100%");
		table.setBorder(0);
		
		if (editMode) {
			table.setBorder(1);
			TR row = new TR();
			table.addElement(row);
			TD cell = new TD();
			row.addElement(cell);
			cell.setBgColor("#ffbbbb");
			
			A link = new A();
            link.setOnClick("OpenLarge(this.href, '1'); return false");
			link.setHref("/hrs-intranet/admin/content/edit.do?section=" +
					this.getSection() +
					"&code=" +
					this.getCode());
			link.setClass("subLink");
			link.addElement("Change Content");
			cell.addElement(link);
		}
		
		ContentHelper contentHelper = new ContentHelper();
		String content = contentHelper.getContent(this.getDomain(), section, code);
		if (editMode) {
			if (StringUtils.isNullOrBlank(content.trim())) {
				content = "<i>No Content</i>";
			}
		}
		
		TR contentRow = new TR();
		table.addElement(contentRow);
		TD contentCell = new TD();
		contentRow.addElement(contentCell);
		contentCell.addElement(content);

		return table.toString();
	}
	

	public String getCode() {
		return code;
	}

	
	public void setCode(String code) {
		this.code = code;
	}

	public String getSection() {
		return section;
	}

	public void setSection(String section) {
		this.section = section;
	}


	public boolean getEdit() {
		boolean edit = false;
		Boolean editContent = (Boolean)this.getRequest().getSessionAttribute("editContent");
		if (editContent != null) {
			edit = editContent.booleanValue();
		}
		return(edit);
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	
}
